package com.wbao.dianniu.update;

import com.wbao.dianniu.data.QaResponse;

import java.util.LinkedList;

/**
 * 问答数据改变管理类
 */
public class QaDataChangeManager {
    private static LinkedList<IQaDataListener> mListener;
    private static QaDataChangeManager instance;

    public static QaDataChangeManager getInstance() {
        if (null == instance) {
            mListener = new LinkedList<IQaDataListener>();
            instance = new QaDataChangeManager();
        }
        return instance;
    }

    private QaDataChangeManager() {
    }

    public interface IQaDataListener {

        /**
         * 新增一条数据更新
         *
         * @param addData
         */
        public void addOneData(QaResponse addData);

        /**
         * 删除更新（By questId）
         *
         * @param questId
         */
        public void removeOneData(int questId);

        /**
         * 点赞数更新
         *
         * @param questId
         * @param isGood
         * @param goodCounts
         */
        public void priseNotify(int questId, int isGood, int goodCounts);

        /**
         * 回复数更新
         *
         * @param questId
         * @param answerCounts
         */
        public void replyNotify(int questId, int answerCounts);

        /**
         * 浏览次数更新
         *
         * @param questId
         * @param visitorCounts
         */
        public void visitorUpdate(int questId, int visitorCounts);

    }

    public void addQaChangeListener(IQaDataListener listener) {
        mListener.add(listener);
    }

    public void removeQaChangeListener(IQaDataListener listener) {
        mListener.remove(listener);
    }

    /**
     * 删除数据通知（通过ID）
     *
     * @param questId
     */
    public void notifyRemove(int questId) {
        for (int i = 0; i < mListener.size(); i++) {
            mListener.get(i).removeOneData(questId);
        }
    }

    /**
     * 点赞数改变通知
     */
    public void priseNotify(int questId, int isGood, int goodCounts) {
        for (int i = 0; i < mListener.size(); i++) {
            mListener.get(i).priseNotify(questId, isGood, goodCounts);
        }
    }

    /**
     * 回复数改变通知
     */
    public void replyNotify(int questId, int answerCounts) {
        for (int i = 0; i < mListener.size(); i++) {
            mListener.get(i).replyNotify(questId, answerCounts);
        }
    }

    public void addOneDataNotify(QaResponse addData) {
        for (int i = 0; i < mListener.size(); i++) {
            mListener.get(i).addOneData(addData);
        }
    }

    public void updateVisitor(int questId, int visitorCounts) {
        for (int i = 0; i < mListener.size(); i++) {
            mListener.get(i).visitorUpdate(questId, visitorCounts);
        }
    }

}
